package com.novel.controller;

import com.github.pagehelper.PageInfo;
import com.novel.service.NovelService;
import com.reading.pojo.Novel;
import com.reading.pojo.NovelVo;
import com.reading.pojo.Novelsimplify;
import com.reading.pojo.user.User;
import com.reading.vo.Rmed;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/novel")
public class NovelController {

    @Resource
    private NovelService novelService;
//    @Resource
//    private RedisTemplate redisTemplate;


    @RequestMapping("list")
    public List<Novel> list(){
        return novelService.list();
    }

    @RequestMapping("list1")
    public Rmed list1(@RequestBody Novel novel,Integer pageNo, Integer pageSize){
        PageInfo<Novel> pageInfo = novelService.list1(novel,pageNo,pageSize);
        return Rmed.suceessData(pageInfo);
    }

    @RequestMapping("find")
    public List<Novelsimplify> find(Integer sort){
        return novelService.find(sort);
    }

    @RequestMapping("findbyid")
    public Rmed findbyid(Integer id){
        return Rmed.suceessData(novelService.findbyid(id));
    }

    @RequestMapping("/findOneNovel")
    public List<Novel> findOneNovel(@RequestBody NovelVo novelVo){
        return novelService.findOneNovel(novelVo);
    }

    @RequestMapping("/findRankingNovel")
    public List<Map<String, Object>> findRankingNovel(){
        return novelService.findRankingNovel();
    }

    @RequestMapping("/findOne")
    public Novel findOne(Integer pid){
        return novelService.findOne(pid);
    }

    @RequestMapping("/add")
    public Rmed add(@RequestBody Novel novel){
        try {
            if (novel.getId()==null){
                novelService.add(novel);
            }else {
                novelService.update(novel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Rmed.error("添加失败");
    }

    @RequestMapping("/findBooks")
    /**
     * 查找书籍
     * 该方法旨在从用户列表中获取书籍信息，并以小说列表的形式返回
     * 如果用户列表为空或第一个用户的书籍信息为空，则返回空列表
     *
     * @return 小说列表，如果找不到则返回空列表
     */
    public List<Novel> findBooks(@RequestParam Integer uid) {
        User user = novelService.findUserById(uid);
       if (user == null) {
           return Collections.emptyList(); // 返回空列表
        }
       // 获取用户的书籍信息
        String books = user.getBooks();
        System.out.println("books: " + books);
        // 检查书籍信息是否为空
        if (books == null || books.isEmpty()) {
            return Collections.emptyList(); // 返回空列表
        }

        // 分割书籍信息获取书籍ID数组
        String[] bookIds = books.split(",");
        System.out.println("bookIds: " + bookIds);
        // 检查书籍ID数组是否为空
        if (bookIds == null || bookIds.length == 0) {
            return Collections.emptyList(); // 返回空列表
        }

        // 创建小说列表以存储找到的小说
        List<Novel> novels = new ArrayList<>();
        // 遍历书籍ID数组，查找对应的小说
        for (String bookId : bookIds) {
            // 检查书籍ID是否非空且不只包含空格
            if (bookId != null && !bookId.trim().isEmpty()) {
                // 根据书籍ID查找小说
                Novel novel = novelService.findById(Integer.parseInt(bookId.trim()));
                // 如果小说存在，则添加到小说列表中
                if (novel != null) {
                    novels.add(novel);
                }
            }
        }

        // 返回小说列表
        return novels;
    }


    @RequestMapping("/findHistory")
    public List<Novel> findHistory(@RequestParam Integer uid) {
        User user = novelService.findUserById(uid);
        if (user == null) {
            return Collections.emptyList(); // 返回空列表
        }
        // 获取用户的书籍信息
        String History = user.getHistory();
        System.out.println("History: " + History);
        // 检查书籍信息是否为空
        if (History == null || History.isEmpty()) {
            return Collections.emptyList(); // 返回空列表
        }

        // 分割书籍信息获取书籍ID数组
        String[] historyIds = History.split(",");
//        System.out.println("bookIds: " + bookIds);
        // 检查书籍ID数组是否为空
        if (historyIds == null || historyIds.length == 0) {
            return Collections.emptyList(); // 返回空列表
        }

        // 创建小说列表以存储找到的小说
        List<Novel> novels = new ArrayList<>();
        // 遍历书籍ID数组，查找对应的小说
        for (String historyId : historyIds) {
            // 检查书籍ID是否非空且不只包含空格
            if (historyId != null && !historyId.trim().isEmpty()) {
                // 根据书籍ID查找小说
                Novel novel = novelService.findById(Integer.parseInt(historyId.trim()));
                // 如果小说存在，则添加到小说列表中
                if (novel != null) {
                    novels.add(novel);
                }
            }
        }

        // 返回小说列表
        return novels;
    }

    //删除
    @RequestMapping("/delete")
    public Rmed delete(Integer id){
        try {
            novelService.delete(id);
            return Rmed.suceessData("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Rmed.error("删除失败");
    }
    @RequestMapping("/updatetwo")
    public Rmed updatetwo(@RequestBody Novel novel){
        try {
            novelService.updatetwo(novel);
            return Rmed.suceessData("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Rmed.error("修改失败");
    }
}
